﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace ProblemsSet
{
    public class Problem_75 : BaseProblem
    {
        public override object GetResult()
        {
            return Solve3();

        }

        private static long Solve1()
        {
            const int max = 1500000;

            var dct = new Dictionary<long, HashSet<Point>>();

            long res = 0;
            var sqr = (long)Math.Sqrt(max) + 1;

            for (long n = 3; n <= sqr; n++)
            {
                long p = 1;
                long l = 0;
                while ((l = n * p * (n - 1)) <= max)
                {
                    var b = p * (n - 1);
                    var a = n * p * (n - 2);
                    p++;
                    if (a % 2 != 0) continue;
                    a /= 2;
                    if (b <= 0) continue;
                    if (a <= 0) continue;
                    if (l - a - b <= 0) continue;
                    var ptn = new Point((int)a, (int)b);
                    var ptn2 = new Point((int)b, (int)a);
                    if (!dct.ContainsKey(l)) dct.Add(l, new HashSet<Point>());
                    if (dct[l].Contains(ptn) || dct[l].Contains(ptn2)) continue;
                    dct[l].Add(ptn);
                }

            }

            foreach (var pair in dct)
            {
                if (pair.Value.Count == 1)
                    res++;
            }

            return res;
        }

        private static long Solve3()
        {
            const int max = 1500000;

            var hs = new HashSet<long>();
            var many = new HashSet<long>();

            for (long l = 12; l <= max; l+=2)
            {
                if (many.Contains(l)) continue;

                for (long a = 1; a < l/2; a++)
                {
                    var bb = l * (l - 2 * a)/(double)2/(double)(l-a);
                    if (a > bb) break;
                    if (Math.Truncate(bb) != bb) continue;
                    if (hs.Contains(l))
                    {
                        hs.Remove(l);
                        long cur = 2;
                        long tmp = 0;
                        while((tmp=l*cur)<=max)
                        {
                            if (!many.Contains(tmp))
                                many.Add(tmp);
                            cur++;
                        }
                        
                        break;
                    }
                    hs.Add(l);

                }
            }
            return hs.Count;
        }

        private static long Solve2()
        {
            const int maxP = 1500000;
            var maxC = maxP / 2;
            var l = new long[maxP];

            long ppt = 0;

            for (long m =2; m <= Math.Sqrt(maxC); m++)
            {
                for (long n = 1+m%2; n <= m-1; m+=2)
                {
                    if (MathLogic.GetNOD(m,n) != 1) continue;
                    var a = m*m - n*n;
                    var b = 2 * m * n;
                    var c = m * m + n * n;
                    if (c > maxC) break;
                    var p = a + b + c;
                    ppt++;
                    long k = 1;
                    if (p > maxP) continue;
                    do
                    {
                        l[k*p - 1]++;
                        k++;
                    } while (k*p <= maxP);
                }
            }

            long ans = 0;

            for (long k = 12; k <= maxP; k+=2)
            {
                if (l[k-1] == 1)
                {
                    ans++;
                }
            }

            return ans;

        }

        public override string Problem
        {
            get
            {
                return @"It turns out that 12 cm is the smallest length of wire that can be bent to form an integer sided right angle triangle in exactly one way, but there are many more examples.

12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)

In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided right angle triangle, and other lengths allow more than one solution to be found; for example, using 120 cm it is possible to form exactly three different integer sided right angle triangles.

120 cm: (30,40,50), (20,48,52), (24,45,51)

Given that L is the length of the wire, for how many values of L  1,500,000 can exactly one integer sided right angle triangle be formed?

Note: This problem has been changed recently, please check that you are using the right parameters.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 161667;
            }
        }

    }
}
